namespace Launcher

import System
import System.Collections
import System.Drawing
import System.Windows.Forms

import ShellLink

import Launcher.Model
import Launcher.View
import Launcher.View.Model

partial class MainForm:
	
	_view as LauncherView
	_hook as UserActivityHook
		
	def constructor():
		// The InitializeComponent() call is required for Windows Forms designer support.
		InitializeComponent()
		
		self.TransparencyKey = Ui.Constants.TransparentColor

		SetupHook()
		SetupModel()
		SetupView()
		
		Visible = false

#region setup

	private def SetupHook():
		_hook = UserActivityHook(false, true)
		_hook.KeyUp += {o, e as KeyEventArgs | ToggleVisibility() if e.KeyCode == Keys.F12}
						
	private def ToggleVisibility():
		self.Visible = not self.Visible
		if self.Opacity == 0.0: self.Opacity = 0.8
		
		if self.Visible:
			self.BringToFront()
		_view.Visible = self.Visible

	private def SetupView():
		_view = LauncherView(self, StartMenuModel.Instance)
		_view.ItemSelected += _OnSelectedItem
		_view.CloseClicked += CloseApplication

	private def _OnSelectedItem(sender, item as LauncherItem):
		item.Model.Execute()

	private def SetupModel():
		StartMenuModel.Instance.Populate()
		StartMenuModel.Instance.PreExecution += _OnPreExecution
	
	private def _OnPreExecution():
		self.Visible = false

#endregion

#region trayicon menu processing
		
	private def ShowToolStripMenuItemClick(sender as object, e as System.EventArgs):
		self.Visible = true
	
	private def ExitMenuItemClick(sender as object, e as System.EventArgs):
		CloseApplication()

#endregion
	
	private def MainFormPaint(sender as object, e as System.Windows.Forms.PaintEventArgs):
		_view.Draw(e, Size)
		
	private def MainFormDeactivate(sender as object, e as System.EventArgs):
		self.Visible = false

	private def MainFormMouseClick(sender as object, e as System.Windows.Forms.MouseEventArgs):
		_view.MouseClick(sender, e)
	
	private def MainFormMouseMove(sender as object, e as System.Windows.Forms.MouseEventArgs):
		_view.MouseMove(sender, e)

	private def MainFormFormClosed(sender as object, e as System.Windows.Forms.FormClosedEventArgs):
		CloseApplication()

	private def CloseApplication():
		RankingFile.Instance.Save("ranking.txt")
		_hook.Stop()
		Application.Exit()
	
	private def MainFormLoad(sender as object, e as System.EventArgs):
		Visible = false
	
[STAThread]
def Main(argv as (string)):
	Application.EnableVisualStyles()
	Application.SetCompatibleTextRenderingDefault(false)
	Application.Run(MainForm())
